
//set appropriate directory.... 

**************
* Program for FDI computation (two good case)
**************
cap program drop compute_TMI_kinks
program define compute_TMI_kinks
syntax , i1_stub(string) i2_stub(string) x1_stub(string) x2_stub(string) n_choices(integer) tmi(string) [addcounts]

	tempname case1 case2 case3a case3b tmitmp

		local i1 `i1_stub'\`n'
	local i2 `i2_stub'\`n'
	local x1 `x1_stub'\`n'
	local x2 `x2_stub'\`n'

	local i1_prime `i1_stub'\`m'
	local i2_prime `i2_stub'\`m'
	local x1_prime `x1_stub'\`m'
	local x2_prime `x2_stub'\`m'

	* for each pair of observations these dummy variables will denote which case applies
	gen `case1' = 0
	gen `case2' = 0
	gen `case3a' = 0
	gen `case3b' = 0

	gen `tmitmp' = . // this will store the value of the current pairwise FDI
	gen `tmi' = -100 // this will be updated for a given individual only when the current pairwise FDI for that individual exceeds all those previously computed for that same individual
	
	if strlen("`addcounts'")!=0 {
		gen ncase1 = 0 // these variables are useful to see how often an individual presents WARP violations
		gen ncase2 = 0
		gen ncase3a = 0
		gen ncase3b = 0
	}
	
	* compute pairwise FDI's for all relevant pairs in a loop 
	forval n = 1/`n_choices' {
		forval m = `=`n'+1'/`n_choices' {
			replace `tmitmp'=.

			replace `case1'  = ((`x1'==`x1_prime')&(`x2'==`x2_prime'))
			replace `case2'  = ((`x1_prime'/`i1')+(`x2_prime'/`i2')>=(`x1'/`i1')+(`x2'/`i2') | (`x1'/`i1_prime')+(`x2'/`i2_prime')>=(`x1_prime'/`i1_prime')+(`x2_prime'/`i2_prime')) & !`case1'
			replace `case3a' = `i2_prime'/`i1_prime'>=`i2'/`i1' & !`case1' & !`case2'
			replace `case3b' = `i2_prime'/`i1_prime'<`i2'/`i1' & !`case1' & !`case2'


			replace `tmitmp' = 0 if `case1'
			replace `tmitmp' = 0 if `case2'
			replace `tmitmp' = 1 + min( (`i2'*(`x1'-`x1_prime'))/(`i1'*(`x2'-`x2_prime')), (`i1_prime'*(`x2_prime'-`x2'))/(`i2_prime'*(`x1_prime'-`x1')) ) if `case3a'
			replace `tmitmp' = 1 + min( (`i2_prime'*(`x1_prime'-`x1'))/(`i1_prime'*(`x2_prime'-`x2')), (`i1'*(`x2'-`x2_prime'))/(`i2'*(`x1'-`x1_prime')) ) if `case3b'			
			
			if strlen("`addcounts'")!=0 {
				replace ncase1 = ncase1+`case1'
				replace ncase2 = ncase2+`case2'
				replace ncase3a = ncase3a+`case3a'
				replace ncase3b = ncase3b+`case3b'
			}
			
			* keep track of the maximum values obtained for each individual so far, this will ultimately be the FDI associated with all observations
			replace `tmi' = max(`tmi',`tmitmp')
		}
	}
end
***********


**************
* compute FDI for CKMS data
**************

qui {

	use "CKM_II_data", clear
	gen id_member = new_nohhold*10 + nomem
	order id_member


	drop if missing(pointx01) // keep the relevant 1182 observations

	rename pointx0? pointx? // e.g. renaming pointx01 in pointx1 so that I can call these in a loop
	rename pointy0? pointy?
	
	compute_TMI_kinks, i1_stub(pointx) i2_stub(pointy) x1_stub(xc) x2_stub(yc) n_choices(25) tmi(tmi_nondiff)
	
	/*Create dataset for EMAX-Matlab*/
	preserve
	forvalues i=1/25 { 
	gen pricex`i'=1/pointx`i'
gen pricey`i'=1/pointy`i'
}
	forvalues i=1/25 { 
	gen xbundle`i'=xc`i'
	gen ybundle`i'=yc`i'
	}
	keep id_member price* xbundle* ybundle*
	export delimited using "EMAX-Matlab/ChoiKarivMullerSilverman_PreparedForAfriat.csv", replace
	restore
	
	
	/* Generate mirror choices for FDI with state independence */
	forval n = 1/25 {
	gen pointx`=25+`n'' = pointy`n'
	gen pointy`=25+`n'' = pointx`n'
	gen xc`=25+`n'' = yc`n'
	gen yc`=25+`n'' = xc`n'
	}
	
	compute_TMI_kinks, i1_stub(pointx) i2_stub(pointy) x1_stub(xc) x2_stub(yc) n_choices(50) tmi(tmi_combined_nondiff)
	//clonevar fosd_tmi = tmi_combined_nondiff
	gen actual=1
	
	save "ChoiKarivMullerSilverman_withTMI.dta", replace
	
	* create a csv dataset to use in Matlab (needs to run only once)
	preserve
		keep id_member point* xc* yc*
		forval n = 1/50 {
		gen pricex`n'=1/pointx`n'
		gen pricey`n'=1/pointy`n'
		}
		drop point*
		order id_member xc* yc* pricex* pricey*
		export delimited using "FDI-EU-Matlab/choices_and_prices.csv", replace
	restore

	
	
	/* Now generate the Bronars data and compute FDIs*/
	
		
	use "CKM_II_data", clear
	
	
	drop if missing(pointx01) // keep the relevant 1182 observations
	rename pointx0? pointx? // e.g. renaming pointx01 in pointx1 so that I can call these in a loop
	rename pointy0? pointy?
	
	keep pointx* pointy*
	expand 20
	gen id_member = _n, before(pointx1)
	
	set seed 12345
	forval n = 1/25 {
		gen xc`n' = runiform() * pointx`n'
		gen yc`n' = pointy`n' - (pointy`n'/pointx`n')*xc`n'
	}

	* compute TMI 
	compute_TMI_kinks, i1_stub(pointx) i2_stub(pointy) x1_stub(xc) x2_stub(yc) n_choices(25) tmi(tmi_nondiff_placebo)
	
	
	/*Create dataset for EMAX-Matlab*/
	preserve
	forvalues i=1/25 { 
	gen pricex`i'=1/pointx`i'
gen pricey`i'=1/pointy`i'
}
	forvalues i=1/25 { 
	gen xbundle`i'=xc`i'
	gen ybundle`i'=yc`i'
	}
	keep id_member price* xbundle* ybundle*
	export delimited using "EMAX-Matlab/ChoiKarivMullerSilverman_randomChoices_PreparedForAfriat.csv", replace
	restore

	
	* generate mirror choices
	forval n = 1/25 {
	gen pointx`=25+`n'' = pointy`n'
	gen pointy`=25+`n'' = pointx`n'
	gen xc`=25+`n'' = yc`n'
	gen yc`=25+`n'' = xc`n'
	}
	
	compute_TMI_kinks, i1_stub(pointx) i2_stub(pointy) x1_stub(xc) x2_stub(yc) n_choices(50) tmi(tmi_combined_nondiff_placebo)
	gen placebo=1
	save "ChoiKarivMullerSilverman_randomChoices.dta", replace
	
	* create a csv dataset to use in Matlab (needs to run only once)
	preserve
		keep id_member point* xc* yc*
		forval n = 1/50 {
		gen pricex`n'=1/pointx`n'
		gen pricey`n'=1/pointy`n'
		}
		drop point*
		order id_member xc* yc* pricex* pricey*
		export delimited using "FDI-EU-Matlab/choices_and_prices_random.csv", replace
	restore
}


	

	
	
	
	
	
